package com.wudimeng.syll.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wudimeng.syll.common.ApiResponse;
import com.wudimeng.syll.constants.Constant;
import com.wudimeng.syll.dao.ICChuKuXinXiDAO;
import com.wudimeng.syll.dao.ICRuKuXinXiDAO;
import com.wudimeng.syll.entity.CChuKuXinXi;
import com.wudimeng.syll.entity.YongHuXinXi;
import com.wudimeng.syll.service.ICChuKuXinXiService;
import com.wudimeng.syll.service.ICRuKuXinXiService;
import com.wudimeng.syll.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;


@Service
public class CChuKuXinXiService implements ICChuKuXinXiService {

    @Autowired
    private ICChuKuXinXiDAO dao;
    @Autowired
    private ICRuKuXinXiDAO rukudao;

    @Override
    public ApiResponse selectCChuKuXinXi(Map<String, String> paramMap,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> maps = dao.selectCChuKuXinXi(paramMap);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(maps);
        return ApiResponse.instance(pageInfo);
    }

    @Override
    @Transactional
    public ApiResponse addCChuKuXinXi(CChuKuXinXi chuKuXinXi) {
        try {
            String guoChanHuoJinKouChuKu = chuKuXinXi.getGuoChanHuoJinKouChuKu();
            String ruKuLiuShuiHao = chuKuXinXi.getRuKuLiuShuiHao();
            if(!StringUtils.isEmpty(ruKuLiuShuiHao)){
                Map<String, Object> rukuxinxi = rukudao.selectCRuKuXinXiByLiuShuiHao(ruKuLiuShuiHao,null);
                BigDecimal rukushuLiang = (BigDecimal) rukuxinxi.get("ShuLiang");
                BigDecimal YiXiaoShouShuLing = (BigDecimal) rukuxinxi.get("YiXiaoShouShuLing");
                BigDecimal kuncun = rukushuLiang.subtract(YiXiaoShouShuLing);
                if(chuKuXinXi.getShuLiang().compareTo(kuncun) == 1){
                    return ApiResponse.error("出库数量不能大于库存数量！");
                }
                //修改上一次入库数量
                rukudao.updCRuKuYiXiaoShouShuLiangADD(chuKuXinXi.getRuKuLiuShuiHao(),chuKuXinXi.getShuLiang());
            }
            chuKuXinXi.setIs_RK("否");
            dao.addCChuKuXinXi(chuKuXinXi);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_INSERT_FAIL);
        }
        return ApiResponse.success(Constant.COMMON_INSERT_SUCCESS);
    }

    @Override
    public ApiResponse updCChuKuXinXi(CChuKuXinXi chuKuXinXi) {
        try {
            CChuKuXinXi chuKuXinXi1 = dao.selectCChuKuXinXiByLiuShuiID(chuKuXinXi.getLiuSHuiID().toString());
            int i = dao.getChuKuXinXiWeiTuoByLiuShuiHao(chuKuXinXi1.getLiuShuiHao());
            if(i > 0){
                return ApiResponse.error("审核已通过不允许修改！");
            }
            GregorianCalendar calendar = new GregorianCalendar();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour >= 17){
                return ApiResponse.error("超过17点不允许修改！");
            }
            String liuShuiID = Integer.toString(chuKuXinXi.getLiuSHuiID());
            //判断是否可以删除  时候有下一级入库单
            Map<String, Object> ruKuXinXi = rukudao.getRuKuXinXiByChuKuLiuShuiId(liuShuiID);
            if(ruKuXinXi != null && ruKuXinXi.size() > 0){
                return ApiResponse.error("下游入库单已产生不允许修改！");
            }
            dao.updCChuKuXinXi(chuKuXinXi);
            return ApiResponse.success(Constant.COMMON_UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_UPDATE_FAIL);
        }
    }

    @Override
    public ApiResponse delCChuKuXinXi(String liuShuiID) {
        try {
            CChuKuXinXi chuKuXinXi = dao.selectCChuKuXinXiByLiuShuiID(liuShuiID);
            int i = dao.getChuKuXinXiWeiTuoByLiuShuiHao(chuKuXinXi.getLiuShuiHao());
            if(i > 0){
                return ApiResponse.error("审核已通过不允许修改！");
            }
            GregorianCalendar calendar = new GregorianCalendar();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour >= 17){
                return ApiResponse.error("超过17点不允许删除！");
            }
            //判断是否可以删除  时候有下一级入库单
            Map<String, Object> ruKuXinXi = rukudao.getRuKuXinXiByChuKuLiuShuiId(liuShuiID);
            if(ruKuXinXi != null && ruKuXinXi.size() > 0){
                return ApiResponse.error("下游入库单已产生不允许删除！");
            }
            if (chuKuXinXi != null) {
                //回退库存
                if("进口出库".equals(chuKuXinXi.getGuoChanHuoJinKouChuKu())) {
                    rukudao.updCRuKuYiXiaoShouShuLiang(chuKuXinXi.getRuKuLiuShuiHao(), chuKuXinXi.getShuLiang());
                }
            } else {
                return ApiResponse.error("出库信息查询失败！");
            }
            dao.addCChuKuXinXiShanChu(chuKuXinXi);
            dao.delCChuKuXinXi(liuShuiID);
            return ApiResponse.success(Constant.COMMON_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_DELETE_FAIL);
        }
    }

    @Override
    public ApiResponse selectCChuKuXinXiByLiuShuiHao(String liuShuiHao,String liuShuiID) {
        return ApiResponse.instance(dao.selectCChuKuXinXiByLiuShuiHao(liuShuiHao,liuShuiID));
    }

    @Override
    public ApiResponse getChuKuLiuShuiHao(String liuShuiHao) {
        List<Map<String, Object>> chuKuLiuShuiHao = dao.getChuKuLiuShuiHao(liuShuiHao);
        if(chuKuLiuShuiHao == null || chuKuLiuShuiHao.size() == 0){
            return ApiResponse.error("未查询到信息");
        }
        return ApiResponse.instance(chuKuLiuShuiHao);
    }

    @Override
    public ApiResponse getZhuiSuXinXiLiuShuiID(String liuShuiID) {
        try {
            return ApiResponse.instance(dao.getZhuiSuXinXiLiuShuiID(liuShuiID));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_OPERATE_FAIL);
        }
    }

    @Override
    public ApiResponse changeIsRk(String liuShuiHao) {
        if(StringUtils.isEmpty(liuShuiHao)){
            return null;
        }
        //查询出库信息
        Map<String, Object> chuKuXinXi = dao.selectCChuKuXinXiByLiuShuiHao(liuShuiHao,null);
        Integer liuSHuiID = (Integer)chuKuXinXi.get("LiuSHuiID");
        CChuKuXinXi chuKu = new CChuKuXinXi();
        chuKu.setLiuSHuiID(liuSHuiID);
        chuKu.setIs_RK("是");
        dao.updCChuKuXinXi(chuKu);
        return null;
    }
}
